package com.starblink.product.detail.dialog

import android.app.Activity
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.basic.ext.appScreenHeight
import com.starblink.product.databinding.DialogTopReviewsBinding
import com.starblink.product.databinding.ItemProductMoreReviewBinding
import com.starblink.product.databinding.ItemProductReviewBinding
import com.starblink.product.detail.dialog.review.ProductReviewHolder
import com.starblink.product.detail.dialog.review.ProductReviewMoreHolder

/**
 * 商品好评列表
 */
class ProductReviewsDialog(
    val context: Activity,
    val productId: String,
    val call: () -> Unit
) : BaseBottomSheetFragment() {
    lateinit var mBinding: DialogTopReviewsBinding
    lateinit var viewModel: StoreReviewVM

    override fun getBindingRootView(): View {
        mBinding = DialogTopReviewsBinding.inflate(layoutInflater)
        return mBinding.root
    }

    private val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.mList.value!!).apply {
            addVhDelegate {
                ProductReviewHolder(
                    ItemProductReviewBinding.inflate(it.inflater, it, false),
                ) {
                }
            }
            addVhDelegate {
                ProductReviewMoreHolder(
                    ItemProductMoreReviewBinding.inflate(it.inflater, it, false),
                ) {
                    call.invoke()
                    dismiss()
                }
            }
        }
    }

    override fun initView() {
        viewModel = ViewModelProvider(requireActivity())[StoreReviewVM::class.java]
        setPeekHeight(appScreenHeight - 44)
        mBinding.close.setOnClickListener {
            dismiss()
        }
        mBinding.recycler.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        viewModel.mList.value = mutableListOf()
        mBinding.recycler.adapter = adapter//单位px
        viewModel.loadList(productId)

        viewModel.mList.observe(this) {
            if (it.isNotEmpty()) {
                adapter.notifyDataSetChanged()
            }
        }

//        mBinding.more.setOnClickListener {
//            call.invoke()
//        }
    }

}